/*
 * @Author: Arthur
 * @Date: 2020-06-17 19:59:01
 * @LastEditors: Arthur
 * @LastEditTime: 2020-06-29 19:00:32
 * @Description: file content
 */
import axios from 'axios'
// import store from '../store/index.js'

import {
  Message,
  MessageBox
} from 'element-ui'
axios.defaults.baseURL = 'https://api.xiaozhucaishui.com'

// 根据开发环境和生产环境不同切换不同url
// if (process.env.NODE_ENV == 'development') {
//     // 开发环境
//     // axios.defaults.baseURL = 'https://dev.xiaozhucaishui.com'
//     axios.defaults.baseURL = 'https://api.xiaozhucaishui.com'

// } else if (process.env.NODE_ENV == 'production') {
//     // 生产环境
//     // axios.defaults.baseURL = 'https://dev.xiaozhucaishui.com'
//     axios.defaults.baseURL = 'https://api.xiaozhucaishui.com'
// }

// axios.defaults.withCredentials = true
// axios.interceptors.request.use(
//     (config) => {
//         // 服务端渲染还没有token
//         try {
//             if (config.notoken && config.notoken === 1) {
//                 config.headers.token = ''
//             } else {
//                 const token = window.localStorage.getItem('token')
//                 token && (config.headers.token = token)
//             }
//         } catch (error) {

//         }

//         if (!config.noLoading) {
//             // store.dispatch('common/addToRequestQueue')
//         }
//         return config
//     },
//     function (error) {
//         // store.dispatch('common/removeFromRequestQueue')
//         return Promise.reject(error)
//     }
// )

axios.interceptors.request.use(request => {
  try {
    if (process.client && request.notoken !== 1) {
      const token = window.localStorage.getItem('token')
      token && (request.headers.token = token)
    }
    if (process.client && request.sign) {
      request.headers.sign = request.sign
    }
    if (process.client && request.is_GTP_TOKEN) {
      const token = window.localStorage.getItem('GTP_TOKEN')
      token && (request.headers.token = token)
    }
    if (process.client && request.searchToken) {
      const token = request.searchToken
      token && (request.headers.token = token)
    }
  } catch (error) {}

  // 测试输出
  //   console.log(
  //     `${new Date().toLocaleString()}【 M=${request.url} 】P=`,
  //     request.params || request.data,
  //   );

  return request;
}, error => {
  return Promise.reject(error);
});
axios.interceptors.response.use(
  (res) => {

    if (res.data.code == 0) {
      if (res.config.url !== '/api/user/getUserActivity' && res.config.url !== '/api/Wendacoupon/coupon_paydetail' && res.config.url !== '/api/gptrecharge/paydetail' && res.config.url !== '/api/recharge/paydetail' && res.config.url !== '/api/resevation/showw_activity')
        Message({
          message: res.data.msg,
          type: 'warning'
        })
    }
    return res
  },
  function (error) {
    // alert('请求超时，请检查网络！')
    // store.dispatch('common/removeFromRequestQueue')
    // 对响应错误做点什么
    if (error && error.response && error.response.data && error.response.data.code == 401) {
      // store.commit('common/setloginShow', true)
      try {
        if (window) {
          // window.localStorage.removeItem('token')
          // window.localStorage.removeItem("userInfo");
          window.localStorage.clear();
          window.location.href = "/login?url=" + window.location.pathname + window.location.search
        }
      } catch (error) {

      }
      Message({
        message: error.response.data.msg,
        type: 'warning'
      })
    }
    return Promise.reject(error)
  }
)
// 处理返回值
function resultHandle(res, resolve) {
  resolve(res)

  // if (res.Code == 200) {
  //     resolve(res)
  // } else {
  //     reject(res.Message)
  // }
}
export function get(url, params, config) {
  return new Promise((resolve, reject) => {
    axios({
      method: "get",
      url: url,
      params: params,
      headers: config,
      responseType: 'blob'
    }).then((res) => {
      resultHandle(res.data, resolve, reject)
    }, err => {
      reject(err)
    })
  })
}
export function post(url, params, config = {
  noLoading: false
}) {
  return new Promise((resolve, reject) => {
    axios.post(url, params, config).then(res => {

      resultHandle(res.data, resolve, reject)
    }, err => {
      reject(err)
    })
  })
}

export function formdataPost(url, params, config = {
  noLoading: false
}) {
  return new Promise((resolve, reject) => {
    let formdata = new FormData()
    formdata.append("file", params.file)

    axios.post(url, formdata, config).then(res => {

      resultHandle(res.data, resolve, reject)
    }, err => {
      reject(err)
    })
  })
}

export const APIURL = axios.defaults.baseURL
